home *** CD-ROM | disk | FTP | other *** search
- /*
- * $Source: /afs/athena.mit.edu/astaff/project/kerberos/src/lib/krb/RCS/in_tkt.c,v $
- * $Author: qjb $
- *
- * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
- * of Technology.
- *
- * For copying and distribution information, please see the file
- * <mit-copyright.h>.
- */
-
- #ifndef lint
- static char *rcsid_in_tkt_c =
- "$Id: in_tkt.c,v 4.9 89/10/25 19:03:35 qjb Exp $";
- #endif /* lint */
-
- #include <conf.h>
- #include <mit_copy.h>
- #include <stdio.h>
- #include <string.h>
- #include <krb.h>
- #ifdef IBMPC
- #include <dos.h>
- #include <fcntl.h>
- #else
- #include <sys/file.h>
- #endif /* IBMPC */
- #include <sys/types.h>
- #include <sys/stat.h>
- #ifdef TKT_SHMEM
- #include <sys/param.h>
- #endif
-
- extern int krb_debug;
-
- /*
- * in_tkt() is used to initialize the ticket store. It creates the
- * file to contain the tickets and writes the given user's name "pname"
- * and instance "pinst" in the file. in_tkt() returns KSUCCESS on
- * success, or KFAILURE if something goes wrong.
- */
-
- in_tkt(pname,pinst)
- char *pname;
- char *pinst;
- {
- #ifdef IBMPC /* use reserved memory as ticket file */
- extern int lock_tkt(tkt_header far*,int);
- extern void unlock_tkt(tkt_header far*);
- tkt_header far *hdr;
- char far *ptr;
- char *value;
- unsigned segment;
-
- if (dest_tkt()!=RET_OK) /* Nuke old tickets */
- return KFAILURE;
- if ((hdr=tkt_ptr())==NULL) {
- #ifdef DEBUG
- if (krb_debug)
- printf("Ticket memory not reserved or"
- "corrupt environment variable\n");
- #endif
- return KFAILURE;
- }
-
- if (!lock_tkt(hdr,1))
- return KFAILURE;
-
- ptr=(char far*)hdr+hdr->eof_ptr;
- if (hdr->eof_ptr+strlen(pname)+strlen(pinst)+2 > hdr->buf_size) {
- if (krb_debug)
- printf("Ticket memory too small\n");
- unlock_tkt(hdr);
- return KFAILURE; /* Reserved memory too small */
- }
- while (*ptr++=*pname++); /* write user's name */
- while (*ptr++=*pinst++); /* write user instance */
- hdr->eof_ptr=FP_OFF(ptr);
- unlock_tkt(hdr);
- return (KSUCCESS);
-
- #else /* !IBMPC */
- int tktfile, creat();
- struct stat buf;
- int count;
- char *file = TKT_FILE;
- int fd;
- register int i;
- char charbuf[BUFSIZ];
- #ifdef TKT_SHMEM
- char shmidname[MAXPATHLEN];
- #endif /* TKT_SHMEM */
-
- if (stat(file,&buf) == 0) {
-
- /* file already exists, and permissions appear ok, so nuke it */
- if ((fd = open(file, O_RDWR, 0)) < 0)
- goto out; /* can't zero it, but we can still try truncating it */
-
- memset(charbuf,0, sizeof(charbuf));
-
- for (i = 0; i < buf.st_size; i += sizeof(charbuf))
- if (write(fd, charbuf, sizeof(charbuf)) != sizeof(charbuf)) {
- (void) close(fd);
- goto out;
- }
-
- (void) close(fd);
- }
- out:
-
- if ((tktfile = creat(file,S_IREAD|S_IWRITE)) < 0) {
- if (krb_debug)
- fprintf(stderr,"Error initializing %s",TKT_FILE);
- return(KFAILURE);
- }
- if (stat(file,&buf) < 0) {
- if (krb_debug)
- fprintf(stderr,"Error initializing %s",TKT_FILE);
- return(KFAILURE);
- }
-
- count = strlen(pname)+1;
- if (write(tktfile,pname,count) != count) {
- (void) close(tktfile);
- return(KFAILURE);
- }
- count = strlen(pinst)+1;
- if (write(tktfile,pinst,count) != count) {
- (void) close(tktfile);
- return(KFAILURE);
- }
- (void) close(tktfile);
- #ifdef TKT_SHMEM
- (void) strcpy(shmidname, file);
- (void) strcat(shmidname, ".shm");
- return(krb_shm_create(shmidname));
- #else /* !TKT_SHMEM */
- return(KSUCCESS);
- #endif /* TKT_SHMEM */
- #endif /* IBMPC */
- }
-